package org.kkk.sandbox.utils;

import org.kkk.sandbox.model.JudgeConfig;
import org.kkk.sandbox.model.ProcessExecuteMessage;
import org.springframework.util.StopWatch;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;


public class ProcessUtils {
    public static boolean isSuccess(int code)
    {
        return code==0;
    }
    public static ProcessExecuteMessage processExecute(Process process)
    {
        return processExecute(process,null);
    }
    public static ProcessExecuteMessage processExecute(Process process, JudgeConfig judgeConfig)
    {
        long time=0;
        int exitCode=0;
        StringBuilder content = new StringBuilder();

        if(process==null)
        {
            throw new RuntimeException("参数不能为空");
        }
        try {
            //计时
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            if(judgeConfig==null)
            {
                exitCode=process.waitFor();
            }
            else{
                process.waitFor(judgeConfig.getTimeLimit()+1, TimeUnit.MILLISECONDS);
            }
            stopWatch.stop();
            StopWatch.TaskInfo taskInfo = stopWatch.getTaskInfo()[stopWatch.getTaskCount() - 1];
            time+=taskInfo.getTimeMillis();
            if(exitCode==0)
            {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
                String lineString="";
                while ((lineString=bufferedReader.readLine())!=null)
                {
                    content.append(lineString);
                }
            }
            else{
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),StandardCharsets.UTF_8));
                String lineString="";
                while ((lineString=bufferedReader.readLine())!=null)
                {
                    content.append(lineString);
                }
                bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream(),StandardCharsets.UTF_8));
                while ((lineString=bufferedReader.readLine())!=null)
                {
                    content.append(lineString);
                }
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            process.destroy();
        }
        return new ProcessExecuteMessage(exitCode,content.toString(),time,0);
    }
    public static ProcessExecuteMessage runInteractProcess(Process process,String param,JudgeConfig judgeConfig)
    {
        OutputStream outputStream = process.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        try {
            outputStreamWriter.write(param);
            outputStreamWriter.flush();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return processExecute(process,judgeConfig);
    }
    public static String readerToStr(InputStream inputStream)
    {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,StandardCharsets.UTF_8));
        String lineString="";
        StringBuilder content=new StringBuilder();
        try {
            while ((lineString = bufferedReader.readLine()) != null) {
                content.append(lineString);
            }
        }
        catch (IOException e)
        {
            return null;
        }
        return content.toString();
    }
}
